<template>
  <div>
    <el-breadcrumb class="app-breadcrumb"
                   separator="/">
      <transition-group name="breadcrumb">
        <el-breadcrumb-item v-for="item in levelList"
                            :key="item.path">
          <router-link :to="item.path">{{item.meta.title}}</router-link>
        </el-breadcrumb-item>
      </transition-group>
    </el-breadcrumb>
    <el-divider></el-divider>
  </div>
</template>

<script>
// 原理：通过监听当前路由的变化，动态更新面包屑的内容。
export default {
  name: 'Breadcrumb',
  data() {
    return {
      levelList: [],
    }
  },
  created() {
    this.getBreadcrumb()
  },

  watch: {
    $route(route) {
      if (route.path.startsWith('/redirect/')) {
        return
      }
      this.getBreadcrumb()
    },
  },
  methods: {
    getBreadcrumb() {
      let matched = this.$route.matched.filter(
        (item) => item.meta && item.meta.title
      )
      this.levelList = matched.filter(
        (item) => item.meta && item.meta.title && item.meta.breadcrumb !== false
      )
    },
  },
}
</script>